package com.ys.service.purchase.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ys.dto.EditDto;
import com.ys.enume.finance.TcwOperateFrozenRunningEnum;
import com.ys.mapper.finance.TcwOperateFrozenRunningMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ys.config.MenuId;
import com.ys.dto.AddDto;
import com.ys.entity.finance.TcwAccountCountForCompany;
import com.ys.entity.finance.TcwOperateFrozenRunning;
import com.ys.entity.flow.YspModel;
import com.ys.entity.purchase.TcgPurchaseAppointOrder;
import com.ys.entity.purchase.TcgPurchaseAppointOrderItem;
import com.ys.entity.purchase.TcgPurchaseAppointOrderProductComponent;
import com.ys.entity.purchase.TcgPurchaseContractItem;
import com.ys.entity.sys.TsUserChange;
import com.ys.entity.sys.YsCol;
import com.ys.entity.sys.YsMenu;
import com.ys.entity.sys.YsProcInst;
import com.ys.enume.flow.FlowEnum;
import com.ys.enume.flow.YsProcInstEnum;
import com.ys.enume.purchase.TcgPurchaseAppointOrderEnum;
import com.ys.mapper.business.DiySqlMapper;
import com.ys.mapper.finance.TcwAccountCountForCompanyMapper;
import com.ys.mapper.purchase.TcgPurchaseAppointOrderItemMapper;
import com.ys.mapper.purchase.TcgPurchaseAppointOrderMapper;
import com.ys.mapper.purchase.TcgPurchaseAppointOrderProductComponentMapper;
import com.ys.service.finance.TcwAccountCountForCompanyService;
import com.ys.service.flow.YspTaskService;
import com.ys.service.purchase.TcgPurchaseAppointOrderService;
import com.ys.util.CheckUtil;
import com.ys.util.shiro.ShiroUtils;
import com.ys.vo.Flow;
import com.ys.vo.R;

@Service("tcgPurchaseAppointOrderService")
public class TcgPurchaseAppointOrderServiceImpl implements TcgPurchaseAppointOrderService {

	@Autowired
	private CheckUtil checkUtil;

	@Autowired
	private YspTaskService yspTaskService;

	@Autowired
	private TcgPurchaseAppointOrderMapper tcgPurchaseAppointOrderMapper;

	@Autowired
	private TcgPurchaseAppointOrderItemMapper tcgPurchaseAppointOrderItemMapper;

	@Autowired
	private TcgPurchaseAppointOrderProductComponentMapper tcgPurchaseAppointOrderProductComponentMapper;

	@Autowired
	private TcwAccountCountForCompanyMapper tcwAccountCountForCompanyMapper;

	@Autowired
	private TcwOperateFrozenRunningMapper tcwOperateFrozenRunningMapper;
	@Autowired
	private TcwAccountCountForCompanyService tcwAccountCountForCompanyService;

	@Autowired
	private DiySqlMapper diySqlMapper;


	private String yzMsg(Map<String, Object> infoMap){
		//验证导入字段
		String backMsg = "";
		//获取pc字段数据
		List<YsCol> listField = new YsCol()
				.where("A.MenuId = "+ MenuId.TCG_PurchaseAppointOrder)
				.setOrderby("A.SortNo ASC")
				.queryList();
		//验证必填字段的非空；其他字段的长度限制、类型
		try {
			backMsg = checkUtil.checkTableColLengthAndDataType(
					"TCG_PurchaseAppointOrder",
					listField,
					infoMap,
					ysColItem->ysColItem.getFormEditRequired()!=null && ysColItem.getFormEditRequired() == 1
			);
			if(backMsg!=null && !"".equals(backMsg)){//若有误，则跳过这条信息
				return backMsg;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 新增
	 */
	@Override
	@Transactional(timeout=120,propagation= Propagation.REQUIRES_NEW)
	public R add(Map<String, Object> params, String subData) throws Exception{
		//验证
		String msg = yzMsg(params);
		if (msg != null){
			return R.error(msg);
		}
		TcgPurchaseAppointOrder tcgPurchaseAppointOrder = JSON.parseObject(JSON.toJSONString(params), TcgPurchaseAppointOrder.class);

		if (new TcgPurchaseAppointOrder().setBid(tcgPurchaseAppointOrder.getBid()).count() != 0){
			return R.error("该单号已存在，核实后再操作!");
		}
		// 验证是否有详情数据
		JSONArray detailJA = JSON.parseArray(subData);
		if (detailJA == null || detailJA.size() == 0) {
			return R.error("详情数据不能为空");
		}

		String loginUserId = ShiroUtils.getUserId();
		TsUserChange tsUserChange = new TsUserChange().setUserId(loginUserId).setStatus(1).queryFirst();

		tcgPurchaseAppointOrder.setStatus(1);
		tcgPurchaseAppointOrder.setCreateChangeId(tsUserChange.getId());
		tcgPurchaseAppointOrder.setEffectStock(FlowEnum.EffectStock.NOT_EFFECT.value);
		tcgPurchaseAppointOrderMapper.insert(tcgPurchaseAppointOrder);

		List<TcgPurchaseAppointOrderItem> t1 = new ArrayList<>();
		for(int i = 0; i < detailJA.size(); i++){
			JSONObject detailJO = detailJA.getJSONObject(i);
			TcgPurchaseAppointOrderItem tcgPurchaseAppointOrderItem = JSON.parseObject(JSON.toJSONString(detailJO), TcgPurchaseAppointOrderItem.class);
			t1.add(tcgPurchaseAppointOrderItem);
		}
		Map<String, List<TcgPurchaseAppointOrderItem>> groupsByCode = t1.stream().collect(Collectors.groupingBy(e->e.getProductCode()));
		List<TcgPurchaseAppointOrderItem> codeList = new ArrayList<>();
		groupsByCode.forEach((k, v) -> {
			codeList.add(v.get(0));
		});
		List<TcgPurchaseAppointOrderItem> t2 = new ArrayList<>();
		for (int i = 0; i < codeList.size(); i++){
			TcgPurchaseAppointOrderItem tcgPurchaseAppointOrderItem = codeList.get(i);
			tcgPurchaseAppointOrderItem.setPbid(tcgPurchaseAppointOrder.getBid())
					.setStatus(1);
			if (!tcgPurchaseAppointOrderItem.getReserveNumber().toString().matches("[0-9]\\d*")){
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
				return R.error("母件预约数量需要是整数，无小数点！");
			}
			tcgPurchaseAppointOrderItemMapper.insert(tcgPurchaseAppointOrderItem);
			t2.add(tcgPurchaseAppointOrderItem);
		}
		for (int i = 0; i < detailJA.size(); i++){
			JSONObject detailJO = detailJA.getJSONObject(i);
			TcgPurchaseAppointOrderItem tcgPurchaseAppointOrderItem = JSON.parseObject(JSON.toJSONString(detailJO), TcgPurchaseAppointOrderItem.class);
			TcgPurchaseAppointOrderProductComponent tcgPurchaseAppointOrderProductComponent = JSON.parseObject(JSON.toJSONString(detailJO), TcgPurchaseAppointOrderProductComponent.class);
		Long t5 =null;
		for(int z=0; z<t2.size(); z++){
				String t3=tcgPurchaseAppointOrderItem.getProductCode();
				String t4=t2.get(z).getProductCode();
				if(t4.equals(t3)){
					 t5=t2.get(z).getId();

				}
			}

			tcgPurchaseAppointOrderProductComponent.setPid(t5)
			.setStatus(1);
		if(tcgPurchaseAppointOrderProductComponent.getSubCodeMerge()!=null) {
				tcgPurchaseAppointOrderProductComponentMapper.insert(tcgPurchaseAppointOrderProductComponent);
		}else {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return R.error("子件合并编码缺失");
		}

		}
		YsMenu ysMenu = new YsMenu().setId(MenuId.TCG_PurchaseAppointOrder).queryFirst();
		if (ysMenu.getFlowModelId().longValue() != 0L) {
			YspModel yspModel = new YspModel().setId(ysMenu.getFlowModelId()).queryFirst();
			String codeModel = yspModel.getCode();
			String createUserId = ShiroUtils.getUserId();
			yspTaskService.init(codeModel, tcgPurchaseAppointOrder.getBid(), createUserId);
		}

		return R.ok(AddDto.gotoEdit("/web/purchase/tcgPurchaseAppointOrder/toEdit?bid=" + tcgPurchaseAppointOrder.getBid()));

	}

	@Override
	@Transactional
	public R edit(Map<String, Object> params, Long menuId,String subData) throws Exception {
		// 验证编码
		String bid = params.get("bid").toString();
		if (new TcgPurchaseAppointOrder().setBid(bid).where("A.Id != '" + params.get("id").toString() + "'").count() > 0) {
			return R.error("商品已存在");
		}
		TcgPurchaseAppointOrder entity = JSON.parseObject(JSON.toJSONString(params), TcgPurchaseAppointOrder.class);
		TcgPurchaseAppointOrder tcgPurchaseAppointOrder = JSON.parseObject(JSON.toJSONString(params), TcgPurchaseAppointOrder.class);

		YsProcInst ysProcInst = new YsProcInst().setBid(entity.getBid()).queryFirst();
		if (ysProcInst == null) {
			return R.error("审批数据不存在，无法编辑");
		} else if (ysProcInst.getStateFlow() != YsProcInstEnum.StateFlow.INIT.value) {
			return R.error("已流转，无法编辑");
		}
		List<TcgPurchaseAppointOrderItem> entity1 = new TcgPurchaseAppointOrderItem()
				.setPbid(entity.getBid())
				.queryList();
		for(int i=0;i<entity1.size();i++){
			diySqlMapper.deleteTcgPurchaseAppointOrderItemPid(entity1.get(i).getId());
		}
		diySqlMapper.deleteTcgPurchaseAppointOrderBid(entity.getBid());
		// 验证是否有详情数据
		JSONArray detailJA = JSON.parseArray(subData);
		if (detailJA == null || detailJA.size() == 0) {
			return R.error("详情数据不能为空");
		}
		List<TcgPurchaseAppointOrderItem> t1 = new ArrayList<>();
		for(int i = 0; i < detailJA.size(); i++){
			JSONObject detailJO = detailJA.getJSONObject(i);
			TcgPurchaseAppointOrderItem tcgPurchaseAppointOrderItem = JSON.parseObject(JSON.toJSONString(detailJO), TcgPurchaseAppointOrderItem.class);
			t1.add(tcgPurchaseAppointOrderItem);
		}
		Map<String, List<TcgPurchaseAppointOrderItem>> groupsByCode = t1.stream().collect(Collectors.groupingBy(e->e.getProductCode()));
		List<TcgPurchaseAppointOrderItem> codeList = new ArrayList<>();
		groupsByCode.forEach((k, v) -> {
			codeList.add(v.get(0));
		});
		List<TcgPurchaseAppointOrderItem> t2 = new ArrayList<>();
		for (int i = 0; i < codeList.size(); i++){
			TcgPurchaseAppointOrderItem tcgPurchaseAppointOrderItem = codeList.get(i);
			tcgPurchaseAppointOrderItem.setPbid(tcgPurchaseAppointOrder.getBid())
					.setStatus(1);
			tcgPurchaseAppointOrderItemMapper.insert(tcgPurchaseAppointOrderItem);
			t2.add(tcgPurchaseAppointOrderItem);
		}
		for (int i = 0; i < detailJA.size(); i++) {
			JSONObject detailJO = detailJA.getJSONObject(i);
			TcgPurchaseAppointOrderItem tcgPurchaseAppointOrderItem = JSON.parseObject(JSON.toJSONString(detailJO), TcgPurchaseAppointOrderItem.class);
			TcgPurchaseAppointOrderProductComponent tcgPurchaseAppointOrderProductComponent = JSON.parseObject(JSON.toJSONString(detailJO), TcgPurchaseAppointOrderProductComponent.class);
			Long t5 = null;
			for (int z = 0; z < t2.size(); z++) {
				String t3 = tcgPurchaseAppointOrderItem.getProductCode();
				String t4 = t2.get(z).getProductCode();
				if (t4.equals(t3)) {
					t5 = t2.get(z).getId();

				}
			}

			tcgPurchaseAppointOrderProductComponent.setPid(t5)
					.setStatus(1);
			if (tcgPurchaseAppointOrderProductComponent.getSubCodeMerge() != null) {
				tcgPurchaseAppointOrderProductComponentMapper.insert(tcgPurchaseAppointOrderProductComponent);
			} else {
				return R.error("子件合并编码缺失");
			}
		}
			tcgPurchaseAppointOrderMapper.update(entity);
		return R.ok(AddDto.gotoEdit("/web/purchase/tcgPurchaseAppointOrder/toEdit?bid=" + tcgPurchaseAppointOrder.getBid()));
	}

	/**
	 * 删除
	 */
	@Override
	public R deleteTrue(Long id) throws Exception {
		if (id == null) {
			return R.error("ID不能为空");
		}

		TcgPurchaseAppointOrder entity = new TcgPurchaseAppointOrder().setId(id).queryFirst();
		if (entity.getEffectStock() == FlowEnum.EffectStock.EFFECTED.value) {
			return R.error("已生效数据无法删除");
		}
//		TcgPurchaseAppointOrderItem entity1 = new TcgPurchaseAppointOrderItem().setId(id).queryFirst();
		List<TcgPurchaseAppointOrderItem> entity1 = new TcgPurchaseAppointOrderItem()
				.setPbid(entity.getBid())
				.queryList();
		for(int i=0;i<entity1.size();i++){
			diySqlMapper.deleteTcgPurchaseAppointOrderItemPid(entity1.get(i).getId());
		}

		tcgPurchaseAppointOrderMapper.deleteTrue(id);

		diySqlMapper.deleteTcgPurchaseAppointOrderBid(entity.getBid());
		String loginUserId = ShiroUtils.getUserId();
		yspTaskService.flowDelete(entity.getBid(), loginUserId);
		return R.ok(EditDto.closeAndReload());
	}

	/**
	 * 删除
	 */
	@Override
	@Transactional
	public R cancel(String bid) throws Exception {
		if (bid == null) {
			return R.error("单号不能为空");
		}

		// 验证是否已生效
		TcgPurchaseAppointOrder entity = new TcgPurchaseAppointOrder().setBid(bid).queryFirst();
		if (entity.getEffectStock() != FlowEnum.EffectStock.EFFECTED.value) {
			return R.error("非已生效数据无法取消");
		}
		// 验证是否已取消
		if (entity.getStateCancel() == TcgPurchaseAppointOrderEnum.StateCancel.YES.value) {
			return R.error("已取消，无法重复操作");
		}
		
		// 验证是否存在采购数据
		Integer count = new TcgPurchaseContractItem()
				.addLeftJoin("TCG_PurchaseAppointOrderProductComponent B ON B.Id = A.OrderProductComponentId")
				.addLeftJoin("TCG_PurchaseAppointOrderItem C ON C.Id = B.Pid")
				.where("C.Pbid = '" + bid + "'")
				.count();
		if (count > 0) {
			return R.error("存在采购数据，无法取消");
		}
		
		entity.setStateCancel(TcgPurchaseAppointOrderEnum.StateCancel.YES.value);

		if(entity.getStateCancel()==1)
		{
			entity.setEffectStock(2);
		}
		tcgPurchaseAppointOrderMapper.update(entity);
		
		String vgsbm = entity.getVgsbm();
		BigDecimal amount = entity.getPreAccount();
		tcwAccountCountForCompanyService.thaw(vgsbm, amount,
				TcwOperateFrozenRunningEnum.DocumentType.TCG_PurchaseAppointOrder.value,
				entity.getId(),
				entity.getBid(),
				null);
		
		return R.ok();
	}

	/**
	 * 预付款退回申请流转
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	@Transactional(timeout=120,propagation= Propagation.REQUIRES_NEW)
	public R flowPass(Long id, String loginUserId) throws Exception {
		TcgPurchaseAppointOrder entity = new TcgPurchaseAppointOrder().setId(id).queryFirst();
		String bid = entity.getBid();
		try {
			YsProcInst ysProcInst = new YsProcInst().setBid(bid).queryFirst();
			TcwAccountCountForCompany account = null;
			List<TcgPurchaseAppointOrder> tcgPurchaseAppointOrderList = null;
			// 若流程是初始化状态，需要验证账户金额
			if (ysProcInst.getStateFlow() == YsProcInstEnum.StateFlow.INIT.value) {
				//查询子件
				tcgPurchaseAppointOrderList = new TcgPurchaseAppointOrder().setBid(bid).queryList();
				for (TcgPurchaseAppointOrder tcgPurchaseAppointOrder : tcgPurchaseAppointOrderList){
					//获取公司编码
					String vGsBm = tcgPurchaseAppointOrder.getVgsbm();
					//根据公司编码获取对应的实时资金表信息
					account = new TcwAccountCountForCompany().setVGsBm(vGsBm).queryFirst();
					//获取对应的实时资金信息
					BigDecimal amountAble = account.getFJe().add(account.getFFzEd()).subtract(account.getFDjJe());
					//判断金额是否大于公司的实时资金
					if (amountAble.compareTo(tcgPurchaseAppointOrder.getPreAccount()) < 0) {
						return R.error("【" + vGsBm + "】账户可用余额不足，无法流转");
					}
				}

			}

			Flow f = yspTaskService.flowPass(bid, loginUserId);
			if (f.getCode() == Flow.Code.FAILED.value) {
				return R.error(f.getMsg());
			} else {
				// 若流程是初始化状态，冻结金额
				if (ysProcInst.getStateFlow() == YsProcInstEnum.StateFlow.INIT.value) {
					for (TcgPurchaseAppointOrder tcgPurchaseAppointOrder : tcgPurchaseAppointOrderList) {
						//获取公司编码
						String vGsBm = tcgPurchaseAppointOrder.getVgsbm();
						//根据公司编码获取对应的实时资金表信息
						account = new TcwAccountCountForCompany().setVGsBm(vGsBm).queryFirst();
						account.setFDjJe(account.getFDjJe().add(tcgPurchaseAppointOrder.getPreAccount()));
						tcwAccountCountForCompanyMapper.update(account);

						tcwOperateFrozenRunningMapper.insert(new TcwOperateFrozenRunning().setSourceId(tcgPurchaseAppointOrder.getId())
								.setChangeFdjje(tcgPurchaseAppointOrder.getPreAccount())
								.setFdjje(account.getFDjJe())
								.setVgsbm(vGsBm)
								.setRemark("预约采购冻结:"+account.getFJe()+",负债额度:"+account.getFFzEd())
						);
					}
				}
				if (f.getState() == Flow.State.FINISH.value) {
					R r = flowFinish(entity);
					if (r.getCode() == 1) {
						return R.error(r.getMsg());
					}
				}
				return R.ok();
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("预约采购流转失败!");
		}
	}

	/**
	 * 预付款退回申请退回
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	@Transactional(timeout=120,propagation= Propagation.REQUIRES_NEW)
	public R flowRefuse(Long id,String reason, String loginUserId) throws Exception {
		TcgPurchaseAppointOrder entity = new TcgPurchaseAppointOrder().setId(id).queryFirst();
		String bid = entity.getBid();
		try {
			R r = yspTaskService.flowRefuse(bid, reason, loginUserId);

			if (r.getCode() == R.Code.SUCCESS.value) {
				YsProcInst ysProcInst = new YsProcInst().setBid(bid).queryFirst();
				// 若退回到制单状态，则解冻账户
				if (ysProcInst.getStateFlow() == YsProcInstEnum.StateFlow.INIT.value) {
					List<TcgPurchaseAppointOrder> tcgPurchaseAppointOrderList = new TcgPurchaseAppointOrder().setBid(bid).queryList();
					for (TcgPurchaseAppointOrder tcgPurchaseAppointOrder : tcgPurchaseAppointOrderList){
						//获取公司编码
						String vGsBm = tcgPurchaseAppointOrder.getVgsbm();
						//根据公司编码获取对应的实时资金表信息
						TcwAccountCountForCompany account = new TcwAccountCountForCompany().setVGsBm(vGsBm).queryFirst();
						account.setFDjJe(account.getFDjJe().subtract(tcgPurchaseAppointOrder.getPreAccount()));
						tcwAccountCountForCompanyMapper.update(account);

						tcwOperateFrozenRunningMapper.insert(new TcwOperateFrozenRunning().setSourceId(tcgPurchaseAppointOrder.getId())
								.setChangeFdjje(tcgPurchaseAppointOrder.getPreAccount())
								.setFdjje(account.getFDjJe())
								.setVgsbm(vGsBm)
								.setRemark("预约付款退回,账户资金:"+account.getFJe()+",负债额度:"+account.getFFzEd()));
					}
				}
			}
			return r;
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("预约付款流转失败!");
		}
	}

	/**
	 * 预付款退回申请驳回
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	@Transactional(timeout=120,propagation= Propagation.REQUIRES_NEW)
	public R flowInit(Long id,String reason, String loginUserId) throws Exception {
		TcgPurchaseAppointOrder entity = new TcgPurchaseAppointOrder().setId(id).queryFirst();
		String bid = entity.getBid();
		try {
			R r = yspTaskService.flowInit(bid, reason, loginUserId);

			if (r.getCode() == R.Code.SUCCESS.value) {
				YsProcInst ysProcInst = new YsProcInst().setBid(bid).queryFirst();
				// 若退回到制单状态，则解冻账户
				if (ysProcInst.getStateFlow() == YsProcInstEnum.StateFlow.INIT.value) {
					List<TcgPurchaseAppointOrder> tcgPurchaseAppointOrderList = new TcgPurchaseAppointOrder().setBid(bid).queryList();
					for (TcgPurchaseAppointOrder tcgPurchaseAppointOrder : tcgPurchaseAppointOrderList){

						//获取公司编码
						String vGsBm = tcgPurchaseAppointOrder.getVgsbm();
						//根据公司编码获取对应的实时资金表信息
						TcwAccountCountForCompany account = new TcwAccountCountForCompany().setVGsBm(vGsBm).queryFirst();
						account.setFDjJe(account.getFDjJe().subtract(tcgPurchaseAppointOrder.getPreAccount()));
						tcwAccountCountForCompanyMapper.update(account);

						tcwOperateFrozenRunningMapper.insert(new TcwOperateFrozenRunning().setSourceId(tcgPurchaseAppointOrder.getId())
								.setChangeFdjje(tcgPurchaseAppointOrder.getPreAccount())
								.setFdjje(account.getFDjJe())
								.setVgsbm(vGsBm)
								.setRemark("预约付款驳回,账户资金:"+account.getFJe()+",负债额度:"+account.getFFzEd()));
					}
				}
			}
			return r;
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("预约付款流转失败!");
		}
	}
	public R flowFinish(TcgPurchaseAppointOrder entity) throws Exception {
		entity.setEffectStock(1);
		tcgPurchaseAppointOrderMapper.update(entity);

		return R.ok();
	}


	//获取列表数据
	@Override
	@Transactional
	public Map<String,Object> getExtraParam( String bid) throws Exception{
		Map<String, Object> entity = new TcgPurchaseAppointOrder().setBid(bid)
				.addLeftJoin("YS_ProcInst Ysp ON Ysp.bid = A.bid")//流程表
				.addField("Ysp.updateTime AS updateTime1," +
						"B.Vname companyName,C.VGsJc vgsjc,D.ProductCode productCode, " +
						"D.ProductName productName,D.CodeMerge codeMerge,D.ReserveNumber reserveNumber," +
						"E.SubCode subCode,E.SubName subName,E.SubCodeMerge subCodeMerge,E.SubBomNumber subBomNumber," +
						"E.SubBomPrice subBomPrice,E.SubReserveNumber subReserveNumber,E.SubTotalPrice subTotalPrice,G.UserId operatorUserId,G.UserName operatorUserName")
				.addLeftJoin("THR_OperateCompanyApply C ON C.VGsBm =A.Vgsbm")
				.addLeftJoin("TCG_PurchaseAppointOrderItem D ON D.Pbid=A.Bid")
				.addLeftJoin("TCG_PurchaseAppointOrderProductComponent E ON E.Pid=D.id")
				.addLeftJoin("TB_CompanyInfo B ON B.Vid=A.CompanyVid")
				.addLeftJoin("TS_UserChange G on G.Id = C.OperatorChangeId")
				.queryFirstMap();
		entity.put("companyName", "福州福沃洋国际商贸有限公司");
		entity.put("companyVid", "C104");
		return entity;
	}
	//获取子列表数据
	@Override
	@Transactional
	public List<Map<String, Object>> getItemList( String bid) throws Exception{
		List<Map<String, Object>> sonlist = new TcgPurchaseAppointOrderItem()
				.setPbid(bid)
				.addField("A.ProductCode productCode, " +
						"A.ProductName productName,A.CodeMerge codeMerge,A.ReserveNumber reserveNumber," +
						"E.SubCode subCode,E.SubName subName,E.SubCodeMerge subCodeMerge,E.SubBomNumber subBomNumber," +
						"E.SubBomPrice subBomPrice,E.SubReserveNumber subReserveNumber,E.SubTotalPrice subTotalPrice")
				.addLeftJoin("TCG_PurchaseAppointOrder B ON B.Bid = A.Pbid")
				.addLeftJoin("TCG_PurchaseAppointOrderProductComponent E ON E.Pid=A.id")
				.queryMap();
		return sonlist;
	}

}
